/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.framework.configuration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Rafael Quintino Gomes Rosa
 */
public abstract class PersistenceUnit {
    public static final String DEFAULT = "default_database";
    public static final String TEST = "test_database";

    public static class HibernateProperty{
        public static final String DRIVER_CLASS = "hibernate.connection.driver_class";
        public static final String URL = "hibernate.connection.url";
        public static final String USERNAME = "hibernate.connection.username";
        public static final String PASSWORD = "hibernate.connection.password";
    }

    public static Connection getConnection(String pu) throws ClassNotFoundException, SQLException{
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(pu);
        Map<String, Object> properties = emf.getProperties();

        String driver = (String)(properties.get(PersistenceUnit.HibernateProperty.DRIVER_CLASS));
        String url = (String)(properties.get(PersistenceUnit.HibernateProperty.URL));
        String username = (String)(properties.get(PersistenceUnit.HibernateProperty.USERNAME));
        String password = (String)(properties.get(PersistenceUnit.HibernateProperty.PASSWORD));

        Class.forName(driver);
        return( DriverManager.getConnection(url, username, password) );
    }
}
